package com.evan.idempotent.autoconfigure;


import com.evan.idempotent.distributedlock.DistributedLock;
import com.evan.idempotent.distributedlock.DistributedLockMysql;
import com.evan.idempotent.distributedlock.DistributedLockRedis;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @Description: 分布式锁自动配置，Bean注入
 * @Author liudeguang
 */
@Configuration
public class DistributedLockAutoConfiguration {

    @Autowired(required = false)
    private DataSource dataSource;
    @Resource
    private RedissonClient redissonClient;

    @Bean("distributedLockMysql")
    public DistributedLock distributedLockMysql() {
        return new DistributedLockMysql(dataSource);
    }

    @Bean("distributedLockRedis")
    @Primary
    public DistributedLock distributedLockRedis() {
        return new DistributedLockRedis(redissonClient);
    }
}
